package com.sdgroup.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.sdgroup.entity.MemberEntity;
import com.sdgroup.service.PageIndexService;
import com.sdgroup.utils.R;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author LiWen
 * @version 1.0
 * @Date: 2020-06-26 14:31
 * @Description: 7DGroup性能演示
 */
@Log4j2
@Controller
public class IndexController {

    @Autowired
    PageIndexService pageIndexService;

    @GetMapping({"/", "/index"})
    public String toindex() {
        return "index";
    }

    @GetMapping("/7d")
    @ResponseBody
    public R indexPage() {
        HashMap<String, Object> map = new HashMap<>();
        log.info("请求成功！");
        map.put("success", "欢迎来到性能实战课堂");
        map.put("Data", new Date());
        map.put("7d", "save data");
        return R.ok().put("data", map);
    }

    /**
     * 运行线程
     *
     * @return
     */
    @GetMapping("/runnable")
    @ResponseBody
    public R runnable() {
        List<MemberEntity> list = pageIndexService.selectAll();
        return R.ok().put("data", list);
    }//


    /**
     * 登陆
     *
     * @param memberEntity
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public R login(MemberEntity memberEntity) {
        log.info("调试成功！:" + memberEntity.getUserName());
        if ("7d".equals(memberEntity.getUserName()) && "123456".equals(memberEntity.getPassWord())) {
            Map<String, String> tokenMap = new HashMap<>();
            tokenMap.put("token", IdUtil.randomUUID() + "-" + memberEntity.getUserName() + "-" + IdUtil.randomUUID());
            tokenMap.put("tokenHead", "Authentication");
            return R.ok().put("data", tokenMap);
        }
        return R.error().put("data", "用户名或者密码失败");
    }


    /**
     * 登陆
     *
     * @param requestBody
     * @return
     */
    @PostMapping(value = "/login/json", produces = "application/json")
    @ResponseBody
    public R login_json(@RequestBody String requestBody) {
        log.info("json数据：{}", requestBody);
        Object parse = JSON.parse(requestBody);
        MemberEntity memberEntity = JSON.parseObject(parse.toString(), MemberEntity.class);
        if ("7d".equals(memberEntity.getUserName()) && "123456".equals(memberEntity.getPassWord())) {
            HashMap<String, Object> tokenMap = new HashMap<>();
            tokenMap.put("token", IdUtil.randomUUID() + "-" + memberEntity.getUserName() + "-" + IdUtil.randomUUID());
            tokenMap.put("tokenHead", "Authentication");
            tokenMap.put("userId", "123132132");
            return R.ok().put("data", tokenMap);
        }
        return R.error().put("data", "用户名或者密码失败");
    }


    //----------关联--------------

    /**
     * 需要关联的数据
     *
     * @param id
     * @return
     */
    @GetMapping("/associated/{id}")
    @ResponseBody
    public R associated(@PathVariable Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(id)) {
            return R.error();
        } else if (id == 1) {
            HttpHeaders headers = new HttpHeaders();
            //根据自己的需要动态添加你想要的content type
            headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
            map.put("des", "关联数据");
            map.put("rew", 6666);
            map.put("header", headers);
            return R.ok().put("data", map);
        } else {
            return R.error().put("data", map);
        }
    }

    /**
     * 需要关联的数据
     *
     * @param id
     * @return
     */
    @GetMapping("/associated/data/{id}")
    @ResponseBody
    public R associatedData(@PathVariable Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(id)) {
            return R.error();
        } else if (id == 6666) {
            map.put("des", "关联数据成功");
            map.put("rew", 8888);
            return R.ok().put("data", map);
        } else {
            return R.error().put("data", map);
        }
    }

    /**
     * 登陆后
     *
     * @param requestBody
     * @return
     */
    @PostMapping(value = "/token", produces = "application/json")
    @ResponseBody
    public R toToken(@RequestBody String requestBody, @RequestHeader Map<String, String> headers) {
        MemberEntity memberEntity = JSON.parseObject(requestBody, MemberEntity.class);
        headers.forEach((key, value) -> {
            // 日志中输出所有请求头
            log.info(String.format("Header '%s' = %s", key, value));
        });

        log.info("关联数据：{}", JSON.toJSON(memberEntity));
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", memberEntity.getUserName());
        map.put("Ok", "tiaoshi");
        return R.ok().put("data", map);
    }

}

//清净法身身体
//圆满报身身相。千百万化身身用